package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcCMFood;
import com.csun.cmny.provider.model.domain.OmcCMFoodInspection;
import com.csun.cmny.provider.model.dto.OmcCMFoodDto;
import com.csun.cmny.provider.model.dto.OmcCMFoodInspectionDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcCMFoodService;
import com.csun.cmny.provider.validator.OmcCMFoodValidator;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/cmFood",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcCMFoodController extends BaseController {
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcCMFoodService omcCMFoodService;

    /**
     * 添加食品检查记录
     */
    @ValidateAnnotation
    @RequestMapping(value = "/inspectionAdd", method = RequestMethod.POST)
    public Wrapper inspectionAdd(@Validated @RequestBody OmcCMFoodInspectionDto omcCMFoodInspectionDto, BindingResult br) {
        int result = omcCMFoodService.saveInspeciton(omcCMFoodInspectionDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 查询食品检查记录
     */
    @ValidateAnnotation
    @RequestMapping(value = "/queryInspection", method = RequestMethod.POST)
    public Wrapper queryInspection( @RequestBody OmcCMFoodInspectionDto omcCMFoodInspectionDto) {
        List<OmcCMFoodInspection> omcCMFoodInspections = omcCMFoodService.queryInspection(omcCMFoodInspectionDto);

        PageInfo<OmcCMFoodInspection> pageInfo = new PageInfo<>(omcCMFoodInspections);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }


    /**
     * 添加食物
     */
    @ValidateAnnotation
    @RequestMapping(value = "/foodAdd", method = RequestMethod.POST)
    public Wrapper foodAdd(@Validated(value = {OmcCMFoodValidator.class}) @RequestBody OmcCMFoodDto omcCMFoodDto, BindingResult br) {
        int result = omcCMFoodService.save(omcCMFoodDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 更新食物
     */
    @ValidateAnnotation
    @RequestMapping(value = "/foodupdate", method = RequestMethod.POST)
    public Wrapper foodupdate( @RequestBody OmcCMFoodDto omcCMFoodDto, BindingResult br) {
        //验证参数
        if(omcCMFoodDto.getId()==null||omcCMFoodDto.getId()<=0){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        //查询该食物记录是否存在
        OmcCMFood omcCMFood = omcCMFoodService.selectByKey(omcCMFoodDto.getId());
        if(PublicUtil.isEmpty(omcCMFood)){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("CM.food.record.not.exist", null), null);
        }

        int result = omcCMFoodService.save(omcCMFoodDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 查询食物
     */
    @ValidateAnnotation
    @RequestMapping(value = "/queryFood",method = RequestMethod.POST)
    public Wrapper queryFood(@RequestBody OmcCMFoodDto omcCMFoodDto){
        List<OmcCMFood> omcCMFoods = omcCMFoodService.queryFood(omcCMFoodDto);

        PageInfo<OmcCMFood> omcCMFoodPageInfo = new PageInfo<>(omcCMFoods);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),omcCMFoodPageInfo);
    }
}
